import request from 'axios';
// AddDialog
const setMainServiceTypeOptions = 'setMainServiceTypeOptions';
const setSubServiceTypeOptions = 'setSubServiceTypeOptions';
// SearchPanel
const setDepartments = 'setDepartments';
// Schedule
const setSchedules = 'setSchedules';
const setDates = 'setDates';
const setPageSize = 'setPageSize';
const setTotal = 'setTotal';
const actions = {
  async addPersonDuty({ state }, duty) {
    try {
      const res = await request({
        url: '/api/addPersonDuty',
        method: 'post',
        data: {
          ...duty,
          ...state.newDutyToken,
          whichWeek: state.whichWeek
        }
      });
      if (res && res.data && res.data.status === 0) {
        return true;
      }
    } catch (err) {
      console.log('添加任务失败', err);
    }
  },
  async deletePersonDuty(ctx, duty) {
    try {
      const res = await request({
        url: '/api/deletePersonDuty',
        method: 'post',
        data: {
          ...duty
        }
      });
      if (res && res.data && res.data.status === 0) {
        return true;
      }
    } catch (err) {
      console.log('删除任务失败', err);
    }
  },

  async updatePersonDuty(ctx, duty) {
    try {
      const res = await request({
        url: '/api/updatePersonDuty',
        method: 'post',
        data: {
          ...duty
        }
      });
      if (res && res.data && res.data.status === 0) {
        return true;
      }
    } catch (err) {
      console.log('请求修改任务失败', err);
    }
  },
  async requestSchedules({ commit, state }) {
    const { whichWeek, page, pageSize, department, name } = state;
    try {
      const res = await request({
        url: '/api/schedule',
        method: 'get',
        params: {
          whichWeek,
          page,
          pageSize,
          department,
          name
        }
      });
      if (res && res.data && res.data.data) {
        const { schedules, dates, pageSize, total } = res.data.data;
        commit(setSchedules, schedules);
        commit(setDates, dates);
        commit(setPageSize, pageSize);
        commit(setTotal, total);
        return true;
      }
    } catch (err) {
      console.log('请求任务数据失败', err);
    }
  },

  async requestServiceTypeOptions({ commit }) {
    try {
      const res = await request({
        url: '/api/serviceTypeOptions',
        method: 'get'
      });
      if (res && res.data && res.data.data) {
        const { mainServiceTypeOptions, subServiceTypeOptions } = res.data.data;
        commit(setMainServiceTypeOptions, mainServiceTypeOptions);
        commit(setSubServiceTypeOptions, subServiceTypeOptions);
        return true;
      }
    } catch (err) {
      console.log('请求服务类型选项失败', err);
    }
  },

  async requestDepartments({ commit }) {
    try {
      const res = await request({
        url: '/api/departments',
        method: 'get'
      });
      if (res && res.data && res.data.data) {
        commit(setDepartments, res.data.data);
      }
    } catch (err) {
      console.log('请求部门数据失败', err);
    }
  }
};

export default actions;
